/**
 * Classname :WebUserDetailsService.java
 *
 * Version information: 1.0
 *
 */
package com.viviet.mobileservices.web.security;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UsernameNotFoundException;
import com.viviet.mobileservices.business.ICustomerService;
import com.viviet.mobileservices.dao.entity.VMCustomer;
import com.viviet.mobileservices.domain.model.WebUserDetails;


/**
*
* Created Jan 07, 2011
*/
public class WebUserDetailsService implements IWebUserDetailsService, InitializingBean {
	private ICustomerService customerService;
	
	/**
	 * @param customerService the customerService to set
	 */
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}


	/*
     * @see org.springframework.security.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
     */
	public synchronized UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException, DataAccessException {
		try {
			VMCustomer customer = customerService.loadCustomerByLoginId(userName);
//			VMCustomer customer = new VMCustomer();
//			customer.setCustomerId("0982345678");
//			customer.setLoginId("admin");
//			customer.setLoginPass("21232f297a57a5a743894a0e4a801fc3");
			
			WebUserDetails user = new WebUserDetails(customer,"ROLE_CUSTOMER");
			return user;
		} catch (NullPointerException ex ){
			throw new UsernameNotFoundException("unrecorgnized username");
		}
    }
	

    /*
     * (non-Javadoc)
     * 
     * @see kr.ac.edu.lab.web.security.IWebUserDetailsService#reloadUserByUsername(java.lang.String)
     */
    public void reloadUserByUsername(String userName) {

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
     */
    public void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
    }
    
}
